<template>
  <div class="app-container">
    <div v-if="crud.props.searchToggle">
      <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <rrOperation/>
    </div>
    <!--工具栏-->
    <div class="head-container">
      <!--      如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">
        <template slot="right">
          <el-button
            class="filter-item"
            size="mini"
            type="success"
            @click="insertDoctor(crud.selections[0])"
            :disabled="crud.selections.length !== 1"
          >
            医生管理
          </el-button>
        </template>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
        :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!--          <el-form-item>-->
          <!--            <template>-->
          <!--              <el-select v-model="form.hpId" placeholder="请选择" v-if="false">-->
          <!--                <el-option-->
          <!--                  v-for="item in options"-->
          <!--                  :key="item.id"-->
          <!--                  :label="item.mnName"-->
          <!--                  :value="item.id">-->
          <!--                </el-option>-->
          <!--              </el-select>-->
          <!--            </template>-->
          <!--          </el-form-item>-->
          <el-form-item label="名称(蒙)" prop="mnName">
            <el-input v-model="form.mnName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(汉)" prop="zhName">
            <el-input v-model="form.zhName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="类型(蒙)" prop="mnType">
            <el-select v-model="form.mnType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.mn_office_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="类型(汉)" prop="zhType">
            <el-select v-model="form.zhType" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.zh_office_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="form.phone" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="地址(蒙)">
            <el-input v-model="form.mnAddress" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="地址(汉)" >
            <el-input v-model="form.zhAddress" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="特色科室">
            <template>
              <el-radio v-model="form.characteristics" label="0">否</el-radio>
              <el-radio v-model="form.characteristics" label="1">是</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="负责人(蒙)" prop="mnPerson">
            <el-input v-model="form.mnPerson" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="负责人(汉)" prop="zhPerson">
            <el-input v-model="form.zhPerson" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="介绍(蒙)" >
            <el-input v-model="form.mnIntroduce" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="介绍(汉)" >
            <el-input v-model="form.zhIntroduce" type="textarea" style="width: 370px;"/>
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="hpName" label="医院"/>
        <el-table-column prop="mnName" label="名称(蒙)"/>
        <el-table-column prop="zhName" label="名称(汉)"/>
        <el-table-column prop="mnType" label="类型(蒙)">
          <template slot-scope="scope">
            {{ dict.label.mn_office_type[scope.row.mnType] }}
          </template>
        </el-table-column>
        <el-table-column prop="zhType" label="类型(汉)">
          <template slot-scope="scope">
            {{ dict.label.zh_office_type[scope.row.zhType] }}
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="电话"/>
        <el-table-column prop="zhAddress" label="地址(汉)"/>
        <el-table-column prop="characteristics" label="特色科室">
          <template slot-scope="scope">
            <span v-if="scope.row.characteristics === '1'">是</span>
            <span v-else>否</span>
          </template>
        </el-table-column>
        <el-table-column prop="zhPerson" label="负责人(汉)"/>
        <el-table-column prop="zhIntroduce" label="介绍(汉)"/>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateTime" label="修改者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','administrativeOffice:edit','administrativeOffice:del'])" label="操作"
                         width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudAdministrativeOffice, {getByName} from '@/api/medicine/administrativeOffice'
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getOfficeByHpId} from "@/api/medicine/administrativeOffice";
import {addOffice, updateOffice, delOffice} from "@/api/medicine/administrativeOffice";

const defaultForm = {
  id: null,
  hpId: null,
  mnName: null,
  mnNameCode: null,
  zhName: null,
  mnType: null,
  mnTypeCode: null,
  zhType: null,
  phone: null,
  mnAddress: null,
  mnAddressCode: null,
  zhAddress: null,
  characteristics: null,
  mnPerson: null,
  mnPersonCode: null,
  zhPerson: null,
  mnIntroduce: null,
  mnIntroduceCode: null,
  zhIntroduce: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  status: null,
  remark: null
}
export default {
  name: 'AdministrativeOffice',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['mn_office_type', 'zh_office_type'],
  cruds() {
    return CRUD({
      title: '科室管理',
      url: 'api/administrativeOffice',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudAdministrativeOffice}
    })
  },
  data() {
    const phoneNumberPattern = /^1[3-9]\d{9}$/;
    return {
      permission: {
        add: ['admin', 'administrativeOffice:add'],
        edit: ['admin', 'administrativeOffice:edit'],
        del: ['admin', 'administrativeOffice:del']
      },
      ids: [],
      hpId: '',
      dialogVisible: false,
      options: [],
      input: '',
      value: '',
      selectedRows: '',
      rules: {
        hpId: [
          {required: true, message: '请选择所属医院', trigger: 'change'}
        ],
        mnName: [
          {required: true, message: '请输入蒙语名称', trigger: 'blur'},
        ],
        zhName: [
          {required: true, message: '请输入汉语名称', trigger: 'blur'},
        ],
        mnType: [
          {required: true, message: '请选择科室类型(蒙)', trigger: 'change'}
        ],
        zhType: [
          {required: true, message: '请选择科室类型(汉)', trigger: 'change'}
        ],
        phone: [
          {required: true, message: '请输入电话号码', trigger: 'blur'},
          {pattern: phoneNumberPattern, message: '请输入有效的电话号码', trigger: 'blur'}
        ],
        mnAddress: [
          {required: true, message: '请输入地址(蒙)', trigger: 'blur'}
        ],
        zhAddress: [
          {required: true, message: '请输入地址(汉)', trigger: 'blur'}
        ],
        mnPerson: [
          {required: true, message: '请输入负责人(蒙)', trigger: 'blur'}
        ],
        zhPerson: [
          {required: true, message: '请输入负责人(汉)', trigger: 'blur'}
        ],
        mnIntroduce: [
          {required: true, message: '请输入介绍(蒙)', trigger: 'blur'}
        ],
        zhIntroduce: [
          {required: true, message: '请输入介绍(汉)', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //  钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params.hpId = this.$route.query.param
      return true
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudAdministrativeOffice.edit(data).then(res => {
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        if (val === 1) {
          data.status = 0;
        } else {
          data.status = 1;
        }
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    insertDoctor(row) {
      // this.$router.push('/medicine/office')
      this.$router.push({path: '/medicine/doctor', query: {param: row.id}})
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });
    }
  },

  mounted(data) {
    defaultForm.hpId = this.$route.query.param
    // console.log(this.$route.query.param)
    // this.crud.params.hpId = this.$route.query.param.id
  }

}
</script>

<style scoped>
.app-container /deep/ .el-table__cell {
  height: 40px;
}

.app-container /deep/ .el-table__body-wrapper .el-table_1_column_3 .cell {
  -webkit-writing-mode: vertical-lr;
  -webkit-text-orientation: sideways-right;
  -o-writing-mode: vertical-lr;
  -moz-writing-mode: vertical-lr;
  -moz-writing-mode: vertical-lr !important;
  writing-mode: vertical-lr !important;
  -webkit-writing-mode: vertical-lr !important;
  -o-writing-mode: vertical-lr !important;
  -ms-writing-mode: tb-lr !important;
}
</style>
